package cn.iocoder.yudao.module.erp.controller.admin.purchaseinorder;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.purchaseinorder.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchaseinorder.PurchaseInOrderDO;
import cn.iocoder.yudao.module.erp.service.purchaseinorder.PurchaseInOrderService;

@Tag(name = "管理后台 - ERP采购入库审批单")
@RestController
@RequestMapping("/erp/purchase-in-order")
@Validated
public class PurchaseInOrderController {

    @Resource
    private PurchaseInOrderService purchaseInOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP采购入库审批单")
    @PreAuthorize("@ss.hasPermission('erp:purchase-in-order:create')")
    public CommonResult<String> createPurchaseInOrder(@Valid @RequestBody PurchaseInOrderSaveReqVO createReqVO) {
        return success(purchaseInOrderService.createPurchaseInOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP采购入库审批单")
    @PreAuthorize("@ss.hasPermission('erp:purchase-in-order:update')")
    public CommonResult<Boolean> updatePurchaseInOrder(@Valid @RequestBody PurchaseInOrderSaveReqVO updateReqVO) {
        purchaseInOrderService.updatePurchaseInOrder(updateReqVO);
        return success(true);
    }

    @PutMapping("/updatePurchaseInOrderReStatus")
    @Operation(summary = "更新ERP采购入库审批单")
    @PreAuthorize("@ss.hasPermission('erp:purchase-in-order:update')")
    public CommonResult<Boolean> updatePurchaseInOrderReStatus(@Valid @RequestBody PurchaseInOrderSupplierSaveReqVO updateReqVO) {
        purchaseInOrderService.updatePurchaseInOrderReStatus(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateStatus")
    @Operation(summary = "更新ERP采购入库审批单")
    @PreAuthorize("@ss.hasPermission('erp:purchase-in-order:update')")
    public CommonResult<Boolean> updateStatusPurchaseInOrder(@Valid @RequestBody PurchaseInOrderSaveReqVO updateReqVO) {
        purchaseInOrderService.updateStatusPurchaseInOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP采购入库审批单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:purchase-in-order:delete')")
    public CommonResult<Boolean> deletePurchaseInOrder(@RequestParam("id") String id) {
        purchaseInOrderService.deletePurchaseInOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP采购入库审批单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:purchase-in-order:query')")
    public CommonResult<PurchaseInOrderSupplierSaveReqVO> getPurchaseInOrder(@RequestParam("id") String id) {
        PurchaseInOrderSupplierSaveReqVO purchaseInOrder = purchaseInOrderService.getPurchaseInOrder(id);
        return success(purchaseInOrder);
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP采购入库审批单分页")
    @PreAuthorize("@ss.hasPermission('erp:purchase-in-order:query')")
    public CommonResult<PageResult<PurchaseInOrderRespVO>> getPurchaseInOrderPage(@Valid PurchaseInOrderPageReqVO pageReqVO) {
        PageResult<PurchaseInOrderDO> pageResult = purchaseInOrderService.getPurchaseInOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PurchaseInOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP采购入库审批单 Excel")
    @PreAuthorize("@ss.hasPermission('erp:purchase-in-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPurchaseInOrderExcel(@Valid PurchaseInOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PurchaseInOrderDO> list = purchaseInOrderService.getPurchaseInOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP采购入库审批单.xls", "数据", PurchaseInOrderRespVO.class,
                        BeanUtils.toBean(list, PurchaseInOrderRespVO.class));
    }

}